/*
 * Memory.cpp
 *
 * Copyright (c) 2009-2011 [BRN] aka. SWW13.
 *
 * This file is part of [BRN]Lib.
 *
 * [BRN]Lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * [BRN]Lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with [BRN]Lib.  If not, see <http ://www.gnu.org/licenses/>.
 */

#include "Memory.h"

namespace BRNlib
{
	namespace Core
	{
		template<class DataType>
		DataType Memory::Read(void *Address)
		{
			DataType Value;
			Read(Address, &Value, sizeof(DataType));

			return Value;
		}
		void Memory::Read(void *Address, void* Data, int Size)
		{
			memcpy_s(Data, Size, Address, Size);
		}

		template<class DataType>
		void Memory::Write(void *Address, DataType Data)
		{
			Write(Address, &Data, sizeof(DataType));
		}
		void Memory::Write(void *Address, void* Data, int Size)
		{
			unsigned long Protection;

			VirtualProtect(Address, Size, PAGE_READWRITE, &Protection);
			memcpy_s(Address, Size, Data, Size);
			VirtualProtect(Address, Size, Protection, 0);
		}

		
		void Memory::NOP(void *Address, int NumberOfBytes)
		{
			BYTE *NOPs = new BYTE[NumberOfBytes];
			int Size = sizeof(BYTE) * NumberOfBytes;
			unsigned long Protection;

			for(int i=0; i < NumberOfBytes; i++)
			{
				NOPs[i] = 0x90;
			}

			VirtualProtect(Address, Size, PAGE_READWRITE, &Protection);
			memcpy_s(Address, Size, NOPs, Size);
			VirtualProtect(Address, Size, Protection, 0);

			delete[] NOPs;
		}

		void GenerateTemplateCode()
		{
			Memory::Read<bool>(NULL);
			Memory::Read<char>(NULL);
			Memory::Read<unsigned char>(NULL);
			Memory::Read<signed   char>(NULL);
			Memory::Read<short>(NULL);
			Memory::Read<unsigned short>(NULL);
			Memory::Read<signed   short>(NULL);
			Memory::Read<int>(NULL);
			Memory::Read<unsigned int>(NULL);
			Memory::Read<signed   int>(NULL);
			Memory::Read<long>(NULL);
			Memory::Read<unsigned long>(NULL);
			Memory::Read<signed   long>(NULL);
			Memory::Read<float>(NULL);
			Memory::Read<double>(NULL);
			Memory::Read<long double>(NULL);

			Memory::Write<bool>(NULL, NULL);
			Memory::Write<char>(NULL, NULL);
			Memory::Write<unsigned char>(NULL, NULL);
			Memory::Write<signed   char>(NULL, NULL);
			Memory::Write<short>(NULL, NULL);
			Memory::Write<unsigned short>(NULL, NULL);
			Memory::Write<signed   short>(NULL, NULL);
			Memory::Write<int>(NULL, NULL);
			Memory::Write<unsigned int>(NULL, NULL);
			Memory::Write<signed   int>(NULL, NULL);
			Memory::Write<long>(NULL, NULL);
			Memory::Write<unsigned long>(NULL, NULL);
			Memory::Write<signed   long>(NULL, NULL);
			Memory::Write<float>(NULL, NULL);
			Memory::Write<double>(NULL, NULL);
			Memory::Write<long double>(NULL, NULL);
		}
	}
}